﻿
Imports osi.root.delegates
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils
Imports osi.service.argument

Public Module _parameter_wrapper
    Public Function parameter_wrapper(Of T)(ByVal p As String,
                                            ByVal f As Func(Of String, var, T, pointer(Of T), event_comb)) _
                                           As Func(Of var, T, pointer(Of T), event_comb)
        assert(Not String.IsNullOrEmpty(p))
        If f Is Nothing Then
            Return Nothing
        Else
            Return Function(v As var, i As T, o As pointer(Of T)) As event_comb
                       Dim ec As event_comb = Nothing
                       Return New event_comb(Function() As Boolean
                                                 Dim s As String = Nothing
                                                 If v Is Nothing Then
                                                     Return False
                                                 ElseIf v.value(p, s) Then
                                                     ec = f(s, v, i, o)
                                                     Return waitfor(ec) AndAlso
                                                            goto_next()
                                                 Else
                                                     Return eva(o, i) AndAlso
                                                            goto_end()
                                                 End If
                                             End Function,
                                             Function() As Boolean
                                                 Return ec.end_result() AndAlso
                                                        goto_end()
                                             End Function)
                   End Function
        End If
    End Function

    Public Function parameter_wrapper(Of T)(ByVal p As String,
                                            ByVal f As _do_val_val_val_ref(Of String, var, T, T, Boolean)) _
                                           As Func(Of var, T, pointer(Of T), event_comb)
        If f Is Nothing Then
            Return Nothing
        Else
            Return parameter_wrapper(p,
                                     Function(s As String, v As var, i As T, o As pointer(Of T)) As event_comb
                                         Return sync_async(Function() As Boolean
                                                               Dim r As T = Nothing
                                                               Return f(s, v, i, r) AndAlso
                                                                      eva(o, r)
                                                           End Function)
                                     End Function)
        End If
    End Function

    Public Function parameter_wrapper(Of T)(ByVal p As String,
                                            ByVal f As Func(Of String, var, T, T)) _
                                           As Func(Of var, T, pointer(Of T), event_comb)
        If f Is Nothing Then
            Return Nothing
        Else
            Return parameter_wrapper(p,
                                     Function(s As String, v As var, i As T, o As pointer(Of T)) As event_comb
                                         Return sync_async(Function() As Boolean
                                                               Dim r As T = Nothing
                                                               r = f(s, v, i)
                                                               Return eva(o, r)
                                                           End Function)
                                     End Function)
        End If
    End Function
End Module
